var app = getApp()
var car = require('../../template/car/breadcar.js')
var category = require('../../template/category/breadcategory.js')
var { formatDate, dayAfter } = require('../../utils/util');
var { newgoods, pname, introlist, getSpecs, goods, ad, newproducts } = require('../../utils/tempdata.js')
var allBread

Page({
  data: {
    topNeedShow: true,
    scrolltop: 0,
    breadType: '1',
    idflag: '',
    breadLoaded: false,
    nproducts: [],
    swiperinfo: {
      hgt: 0,
      indicatorDots: true,
      autoplay: true,
      circular: true,
      interval: 5000,
      duration: 500,
    },
    couponImg:''
  },

  onLoad: function () {
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        var swiper_hgt = 400 * res.windowWidth / 800;
        var swiperinfo = that.data["swiperinfo"];
        swiperinfo.hgt = swiper_hgt;
        that.setData({
          "swiperinfo": swiperinfo
        });
      },
    });
  },

  onShow: function () {
    app.ready(() => {
      this.handleReady();
    });
  },

  handleReady: function () {
    var url = '/index.php/api/Index/Index';
    app.request(url,{}).then(this.loadIndexDataCallBack.bind(this));
  },
  loadIndexDataCallBack:function(res){
    console.log(res);
    if(res.status != 'ok'){
      return;
    }
    var cpImg = ''
    if (res.result.yhad.length > 0){
      cpImg = app.globalData.webUrl + res.result.yhad[0].ad_code;
    }     

    var tjimgs = res.result.yxad;
    for(var i=0;i<tjimgs.length;i++){
      tjimgs[i].ad_code = app.globalData.webUrl + tjimgs[i].ad_code;
      if (tjimgs[i].goods_name.length > 20){
        tjimgs[i].goods_name = tjimgs[i].goods_name.substr(0,20) + '...';
      }
    }
    var imgs = res.result.ad;
    for(var i=0;i<imgs.length;i++){
      imgs[i].ad_code = app.globalData.webUrl + imgs[i].ad_code;
    }

    var newpros = res.result.tjad;
    for(var i=0;i<newpros.length;i++){
      newpros[i].ad_code = app.globalData.webUrl + newpros[i].ad_code;
      newpros[i].specs = newpros[i].keywords.split(' ');
    }

    var xwad = res.result.xwad;
    for(var i=0;i<xwad.length;i++){
      xwad[i].ad_code = app.globalData.webUrl + '/' + xwad[i].ad_code;
    }
    this.setData({
      breadLoaded: true,
      tjimgs: tjimgs,
      imgs: imgs,
      nproducts: newpros,
      xinwen:xwad,
      couponImg:cpImg
    });
  },
  onShareAppMessage: function () {
    return app.shareToIndex();
  },

  hideLoading: function () {

  },
  goodsScroll: function (e) {
    var topNeedShow = true
    if (e.detail.deltaY < 0 && e.detail.scrollTop > 20) {
      topNeedShow = false
    }
    if (this.data.cgr_show === true) {
      topNeedShow = true
    }
    if (this.data.topNeedShow !== topNeedShow) {
      this.setData({
        topNeedShow: topNeedShow
      })
    }
  },

  showCar: car.showCar,
  submitOrder: car.submitOrder,
  closeCar: car.closeCar,
  toggleCar: car.toggleCar,

  showCategory: category.show,
  closeCategory: category.close,
  toggleCategory: category.toggle,
  chooseCategory: category.choose,

  toPageTop: function () {
    this.setData({
      scrolltop: 0
    })
  },
  toGoodsDetail: function (e) {
    var goodsid = e.currentTarget.dataset.goodsid;
    var page_url = '../store/goodsdetail/goodsdetail?goodsid=' + goodsid;
    wx.navigateTo({
      url: page_url,
    });
  },
  closeAllOverlay: function () {
    var bread = this.data.breadlist
    for (var i = 0; i < bread.length; i++) {
      bread[i].overlay = '0'
    }

    this.setData({
      showOverlay: false
    })

    setTimeout(() => {
      this.setData({
        breadlist: bread
      })
    }, 300)
  },
  wxSearchTab: function () {
    wx.navigateTo({
      url: '../store/search/search?target=goods',
    });
  },
  toNewsDetail:function(e){
    var newsid = e.currentTarget.dataset.newsid;
    var path = '../news/news?newsid=' + newsid;
    wx.navigateTo({
      url: path,
    })
  },
  toCouponCenter:function(e){
    wx.navigateTo({
      url: '../my/coupon/getcoupon',
    });
  }
})
